﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using BlueLotus7.Common;

namespace BlueLotus7.Contract
{
    public class ConfigurationValue
    {
        protected string valueString;
        protected object valueObject;

        protected ConfigurationValue(string valueString)
        {
            this.valueString = valueString;
            InitializeValueObject();
        }

        protected virtual void InitializeValueObject()
        {
        }

        public virtual void SetValue(string valueString)
        {
            this.valueString = valueString;
        }

        public override string ToString()
        {
            return valueString;
        }

        public Type GetValueType()
        {
            return valueObject.GetType();
        }

        public static ConfigurationValue GenerateConfigurationValue(string type, string valueString)
        {
            ConfigurationValue result = null;
            Validator.VerifyNullOrEmptyString(type,"type");

            switch (type.ToLower())
            {
                case "int":
                case "int32":
                    result = new Int32ConfigurationValue(valueString);
                    break;
                case "float":
                case "single":
                    result = new FloatConfigurationValue(valueString);
                    break;
                case "double":
                    result = new DoubleConfigurationValue(valueString);
                    break;
                case "long":
                case "int64":
                    result = new Int64ConfigurationValue(valueString);
                    break;
                case "datetime":
                    result = new DateTimeConfigurationValue(valueString);
                    break;
                case "timespan":
                    result = new TimeSpanConfigurationValue(valueString);
                    break;
                case "string":
                    result = new StringConfigurationValue(valueString);
                    break;
                case "guid":
                    result = new GuidConfigurationValue(valueString);
                    break;
                default:
                    break;
            }

            return result;
        }

        public static ConfigurationValue GenerateConfigurationValue(XmlNode configurationNode)
        {
            ConfigurationValue result = null;

            if (configurationNode != null)
            {
                string type = configurationNode.Attributes["type"].Value;
                string value = configurationNode.InnerText;
                result = GenerateConfigurationValue(type, value);
            }

            return result;
        }

        public void FillIntoXmlElement(XmlElement xmlElement)
        {
            if (xmlElement != null)
            {
                xmlElement.SetAttribute("type", valueObject.GetType().Name.ToLower());
                xmlElement.InnerText = valueString;
            }
        }
    }
}
